/*
 * Copyright 2008 Hector Francisco Hernandez
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

var bananaShoot;
var bananaReturnFunction;
var bananaShootResult;
var bananaLastPosition;

function bananaInit(canvas)
{
  canvas.innerHTML += "<img name=\"bananaImage\" " +
      "style=\"position:absolute; visibility:hidden\" " +
      "src=\"banana.gif\">";
}

function bananaDoShoot(x0, y0, angle, velocity, returnFunction)
{
  bananaReturnFunction = returnFunction;
  ret = bananaCalculateShoot(x0, y0, angle, velocity);
  bananaDrawShoot(ret[0], ret[1]);
}

function bananaCalculateShoot(x0, y0, angle, velocity)
{
  angle = angle / 180 * 3.1416;

  velX = velocity / 10 * Math.cos(angle);
  velY = -(velocity / 10 * Math.sin(angle));
  x = x0;
  y = y0;

  shoot = new Array();
  shoot.push([x, y]);

  for (;;) {
    dX = (velX < 0 ? -1 : 1) * (Math.abs(velX) < Math.abs(velY) ?
        Math.abs(velX / velY) : 1);
    dY = (velY < 0 ? -1 : 1) * (Math.abs(velY) < Math.abs(velX) ?
        Math.abs(velY / velX) : 1);
    maxVel = Math.max(Math.abs(velX), Math.abs(velY));

    for (i = 0; i < maxVel; i++) {
      x += dX;
      y += dY;

      if (x < 0 || x > 640 || y > 480) {
        shoot.push([x, y]);
        return [shoot, 0];
      }
      else if (buildingIsIn(x, y) && !impactIsIn(x, y)) {
        shoot.push([x, y]);
        return [shoot, 1];
      }
      else if (gorillaLeftIsIn(x, y)) {
        shoot.push([x, y]);
        return [shoot, 2];
      }
      else if (gorillaRightIsIn(x, y)) {
        shoot.push([x, y]);
        return [shoot, 3];
      }
    }

    shoot.push([x, y]);

    velY += 3;
  }
}

function bananaDrawShoot(shoot, shootResult)
{
  bananaShoot = shoot;
  bananaShootResult = shootResult;
  document.bananaImage.style.visibility = "visible";
  bananaDrawShootIteration();
}

function bananaDrawShootIteration() {
  position = bananaShoot.shift();

  if (position == null) {
    document.bananaImage.style.visibility = "hidden";

    if (bananaShootResult == 1) {
      impactCreate(bananaLastPosition[0], bananaLastPosition[1]);
      return bananaReturnFunction(0);
    }
    else if (bananaShootResult == 2) {
      gorillaLeftExplode();
      return bananaReturnFunction(1);
    }
    else if (bananaShootResult == 3) {
      gorillaRightExplode();
      return bananaReturnFunction(2);
    }

    return bananaReturnFunction(0);
  }

  bananaLastPosition = position;
  document.bananaImage.style.left = (position[0] - 2) + "px";
  document.bananaImage.style.top = (position[1] - 2) + "px";
  setTimeout("bananaDrawShootIteration()", 100);
}

